package com.acme.bankapp.service.bank.email;

import com.acme.bankapp.domain.bank.Client;

public class EmailService {
    private EmailQueue emailQueue = new EmailQueue();
    private volatile boolean alive = true;

    public EmailService() {
        new Thread(new MultiThreadLogic()).start();
    }

    public void sendNotificationMail(Client client, String emailAddress) {
        if (alive) {
            emailQueue.enqueue(new Email(client, emailAddress));
        } else {
            System.out.println("Sorry, Email service was closed.");
        }
    }

    public void close() {
        alive = false;
    }

    private class MultiThreadLogic implements Runnable {
        @Override
        public void run() {
            while (alive || !emailQueue.isEmpty()) {
                System.out.println("Notification email for client " + emailQueue.dequeue() + " was sent");
            }
        }
    }
}
